import moment from 'moment';
import * as imageConversion from 'image-conversion';
export const utils = () => {

    let toCamelCase = (str) => {
        str = str.toLowerCase()
        return str.replace(/_([a-z])/g, function (match, letter) {
            return letter.toUpperCase();
        });
    }

    let compressImage = async (file) => {
        return new Promise((resolve, reject)=>{
            imageConversion.compressAccurately(file, 100).then(res => {
                resolve(res);
            }).catch(e => {
                reject(e);
            })
        })
    }

    let handleOperators = (operators, title, path) => {
        let canPush = true;
        operators.forEach(item => {
            item.checked = false;
            if (item.path === path) {
                item.checked = true;
                canPush = false;
            }
        });
        if (canPush) {
            let id = operators[operators.length - 1].id;
            operators.push({
                id: id + 1, title: title, path: path, canClose: true, checked: true
            });
        }
        localStorage.setItem("operators", JSON.stringify(operators));
        localStorage.setItem("curPath", path);
        return operators;
    }

    let copyTextToClipboard = async (text) => {
        await navigator.clipboard.writeText(text);
        return true;
    }


    return {
        toCamelCase,
        compressImage,
        handleOperators,
        copyTextToClipboard
    }

}
